<!doctype html>
<html lang="zh-CN" th:replace="admin-layout :: layout(~{::title}, ~{}, ~{::script}, ~{::body}, 'menu-list')"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>菜单列表</title>
    <script>
        var $table = $("#table");
        var $remove = $("#removeBtn");
        var $update = $("#updateBtn");
        var $setSortBtn = $("#setSortBtn");
        var $search = $("#searchBtn");
        var selections = []

        //$(this).serialize()
        $(function () {
            tableList(columns)
        })

        var columns = [{
            checkbox: true
        }, {
            field: 'name',
            title: '菜单名称',
        }, {
            field: 'link',
            title: '菜单链接',
        }, {
            field: 'mark',
            title: '菜单标记',
        }, {
            field: 'sort',
            title: '菜单顺序',
        }, {
            field: 'status',
            title: '菜单显示',
            formatter: statusFormatter
        }]

        function statusFormatter(value, row, index) {
            return '<input type="checkbox" '+(value==0?"checked":"")+' class="statusCheckbox fyl-cp" data-id="'+row.id+'">';
        }

        $remove.click(function () {
            confirmAlert(function () {
                ajaxJson("/admin/menu/ajax/delete", {"ids": selections.toString()}, function (res) {
                    if (res.status) {
                        queryLeftList();
                        $table.bootstrapTable('refresh')
                        $remove.prop('disabled', true);
                        $update.prop('disabled', true);
                        $setSortBtn.prop('disabled', true);
                    }
                })
            })
        })

        $update.click(function () {
            var ids = getIdSelections()
            if (ids){
                location.href = "/admin/menu/save?id=" + ids[0]
            }
        })

        $setSortBtn.click(function () {
            var title = "设置菜单顺序";
            var id = selections[0];
            var v = getSortSelections()[0];
            layer.prompt({
                value: v,
                title: title,
            }, function (value, index, elem) {
                if (isNumber(value)) {
                    ajaxJson("/admin/menu/ajax/updateSort", {"id": id, "sort": value}, function (res) {
                        if (res.status) {
                            queryLeftList();
                            $table.bootstrapTable('refresh')
                            $remove.prop('disabled', true);
                            $update.prop('disabled', true);
                            $setSortBtn.prop('disabled', true);
                            layer.close(index);
                        }
                    })
                } else {
                    msgTips("请输入整数")
                }
            });
        })

        function getIdSelections() {
            return $.map($table.bootstrapTable('getSelections'), function (row) {
                return row.id
            })
        }

        function getSortSelections() {
            return $.map($table.bootstrapTable('getSelections'), function (row) {
                return row.sort
            })
        }

        $table.on('check.bs.table uncheck.bs.table ' +
            'check-all.bs.table uncheck-all.bs.table',
            function () {
                $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
                $update.prop('disabled', !$table.bootstrapTable('getSelections').length);
                $setSortBtn.prop('disabled', !$table.bootstrapTable('getSelections').length);

                // save your data, here just save the current page
                selections = getIdSelections()
                // push or splice the selections if you want to save all data selections
            })

        $search.click(function () {
            $("input[name='labelNumber']").val(1);
            $table.bootstrapTable('refresh')
        })

        $(document).on("click", ".statusCheckbox", function () {
            var id = $(this).data("id");
            var newVar = $(this).is(":checked");
            ajaxJson("/admin/menu/ajax/updateStatus", {"id": id, "status": newVar ? 0 : 1}, function (res) {
                if (res.status) {
                    queryLeftList();
                }
            })
        })

        function queryLeftList() {
            // $table.bootstrapTable('refresh')
            $.post("/admin/menu/child/left/li", function (result) {//无刷新提交表单
                $("#leftLi").html(result);
            })
        }
    </script>
</head>
<body>
<!--<ol class="breadcrumb">
    <li class="active">菜单列表</li>
</ol>-->
<div class="">
    <form id="toolbar">
        <div class="row">
            <div class="col-sm-4 col-md-4 col-lg-3">
                <input type="text" class="form-control fyl-clear" name="name"
                       placeholder="菜单名称">
            </div>
            <div class="col-sm-4 col-md-4 col-lg-3">
                <a href="javascript:void(0)" class="btn btn-primary" id="searchBtn">查询</a>
                <a href="javascript:void(0)" class="btn btn-warning" id="clearSearchBtn">清空</a>
            </div>
        </div>
    </form>
</div>
<hr>
<div class="fyl-mb20">
    <a th:href="@{/admin/menu/save}" class="btn btn-primary">添加</a>
    <button type="button" class="btn btn-primary" id="updateBtn" disabled>修改</button>
    <button type="button" class="btn btn-primary" id="setSortBtn" disabled>设置顺序</button>
    <button type="button" class="btn btn-danger" id="removeBtn" disabled>删除</button>
</div>
<table id="table" data-url="/admin/menu/ajax/list"></table>
</body>
</html>